﻿Imports System
Imports System.Runtime.InteropServices
Imports System.Workflow.ComponentModel
Imports System.Workflow.ComponentModel.Compiler

Namespace System.Workflow.Activities
    <ComVisible(False)> _
    Friend NotInheritable Class StateFinalizationValidator
        Inherits CompositeActivityValidator
        ' Methods
        Public Overrides Function Validate(ByVal manager As ValidationManager, ByVal obj As Object) As ValidationErrorCollection
            Dim errors As ValidationErrorCollection = MyBase.Validate(manager, obj)
            Dim compositeActivity As StateFinalizationActivity = [TryCast](obj,StateFinalizationActivity)
            If (compositeActivity Is Nothing) Then
                Throw New ArgumentException(SR.GetString("Error_UnexpectedArgumentType", New Object() { GetType(StateFinalizationActivity).FullName }), "obj")
            End If
            Dim parent As StateActivity = [TryCast](compositeActivity.Parent,StateActivity)
            If (parent Is Nothing) Then
                errors.Add(New ValidationError(SR.GetError_StateFinalizationParentNotState, &H606))
                Return errors
            End If
            Dim activity3 As Activity
            For Each activity3 In parent.EnabledActivities
                Dim activity4 As StateFinalizationActivity = [TryCast](activity3,StateFinalizationActivity)
                If ((Not activity4 Is Nothing) AndAlso (Not activity4 Is compositeActivity)) Then
                    errors.Add(New ValidationError(SR.GetError_MultipleStateFinalizationActivities, &H61A))
                    Exit For
                End If
            Next
            If StateMachineHelpers.ContainsEventActivity(compositeActivity) Then
                errors.Add(New ValidationError(SR.GetError_EventActivityNotValidInStateFinalization, &H603))
            End If
            Return errors
        End Function

    End Class
End Namespace

